/****************************************************
 * @class MaReportGridPanel
 * @extends Ext.Window
 ***************************************************/
	MaReportGridPanel = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 100;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/maReportSummary.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'product_FK'
				},[
					{name: 'product_FK'},
					{name: 'prodVersion_FK'},
					{name: 'ma_status'},
					{name: 'spec_status'},
					{name: 'more'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'product_FK',
		           sortable: true,
		           width: 100
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        },{
		           header: "采购规格书完成率",
		           dataIndex: 'spec_status',
		           width: 50
		        },{
		           header: "物料承认书完成率",
		           dataIndex: 'ma_status',
		           width: 50
		        },{
		           header: "明细",
		           dataIndex: 'more',
		           width: 30,
		           renderer: renderMaOpen
		        }]);
		        	        
			MaReportGridPanel.superclass.constructor.call(this, {
				enableColumnMove: false,
				enableHdMenu: false,
			    loadMask: {msg: "信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: true,
				autoHeight: true,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
				cm: this.cm,
				trackMouseOver: false,
				bbar: new Ext.PagingToolbar({
					pageSize: 100,
					store: this.store,
					displayInfo: true
				})
			});
		},
		load: function(c){
			this.condition.queryList = c;
			this.store.load();
		}
	});
	
	function renderMaOpen(v,p,r)
	{
		v = "<a href='javascript:openMaDetail(\""+r.get("product_FK")+"\",\""+r.get("prodVersion_FK")+"\")'>"+v+"</a>";
		return v;
	}
	
	function openMaDetail(product_FK,prodVersion_FK)
	{
		window.open(window.basePath+"pages/pdm/MaDetailPage.jsp?product_FK="+product_FK+"&prodVersion_FK="+prodVersion_FK,"","toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=yes");
	}
	
/****************************************************
 * @class MaParamGrid
 * @extends Ext.grid.GridPanel
 ***************************************************/
	MaParamGrid = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		btn_auth: null,
		win_MaProduVer: null,
		hidden_MaParam: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 200;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,   
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/getGpParam.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'product_FK'
				},[
					{name: 'product_FK'},
					{name: 'prodVersion_FK'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'product_FK',
		           sortable: true,
		           width: 80
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        }]);
	        
			MaParamGrid.superclass.constructor.call(this, {
				enableColumnMove: false,
				enableHdMenu: false,
			    loadMask: {msg: "信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: true,
				autoHeight: true,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
				cm: this.cm,
				trackMouseOver: false,
				tbar: ['->', {
					text: "添加",
					scope: this,
					iconCls: 'add',
					tooltip: "选择条件",
					hidden: (this.btn_auth=="false"),
					handler: function(){
						if(Ext.isEmpty(this.win_MaProduVer))
						{
							var gp_MaProduVer = new MaProduVerGrid();
							this.win_MaProduVer = new MaProduVerWindow({produVerGrid: gp_MaProduVer, hidden_MaParam: this.hidden_MaParam});
						}
						
						this.win_MaProduVer.show();
					}
				},'-',{
					text: "删除",
					scope: this,
					iconCls: 'delete',
					hidden: ( this.btn_auth == "false" ),
					handler: function(){
						var record = this.getSelectionModel().getSelected();
						if(Ext.isEmpty(record))
						{
							Ext.Msg.alert("提示", "请单击选中需要删除的行！");
						}else{
							this.store.remove(record);
							
							var n = this.getStore().getCount();
							var gp_MaReport = Ext.getCmp("gp_MaReport");
							
							var condition = "";
							if(n == 0)
							{
								condition = "[]";
								Ext.fly(this.hidden_MaParam).dom.value = condition;
							}else{
								condition = "[";
								
								for(var i=0 ; i< n ; i++)
								{
									var r = this.getStore().getAt(i);
									
									var pro = r.get("product_FK");
									var proVer = r.get("prodVersion_FK");
									
									condition = condition + "{'product_FK':'"+pro+"','prodVersion_FK':'"+proVer+"'}";
									
									if(i!=n-1) condition = condition + ",";
								}
								condition = condition + "]";
								
								Ext.fly(this.hidden_MaParam).dom.value = condition;
								
							}
							gp_MaReport.load(condition);
						}
					}
				}]
			});
		},
		load: function(c){
			this.condition.querylist = c;
			this.store.load();
		}
	});

/****************************************************
 * @class MaProduVerWindow
 * @extends Ext.Window
 ***************************************************/
	MaProduVerWindow = Ext.extend(Ext.Window,{
		produVerGrid: null,
		hidden_MaParam: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			MaProduVerWindow.superclass.constructor.call(this, {
				title: "成品料号选择窗口",
				layout: 'fit',
				width: 360,
				height: 420,
				modal: true,
				plain: true,
				resizable: false,
				closeAction: 'hide',
				buttonAlign: 'center',
				items: this.produVerGrid,
				buttons: [{
					text: "确定",
					scope: this,
					handler: function(){
						var record = this.produVerGrid.getSelectionModel().getSelected();
						if(Ext.isEmpty(record))
						{
							Ext.Msg.alert("提示","请选择相应的成品料号！");
						}else{
							//var param = Ext.fly("hidden_GpParam").dom.value;
							var product_FK = record.get("product_FK");
							var prodVersion_FK = record.get("prodVersion_FK");
							
							var gp_MaParam = Ext.getCmp("gp_MaParam");
							var gp_MaReport = Ext.getCmp("gp_MaReport");
							
							var n = gp_MaParam.getStore().getCount();
							var condition = "";
							
							if(n==0)
							{
								condition = "[{'product_FK':'"+product_FK+"','prodVersion_FK':'"+prodVersion_FK+"'}]";
								//gp_GpParam.load(condition);
								Ext.fly(this.hidden_MaParam).dom.value = condition;
							}else{
								condition = "[";
								
								for(var i=0 ; i< n ; i++)
								{
									var r = gp_MaParam.getStore().getAt(i);
									
									var pro = r.get("product_FK");
									var proVer = r.get("prodVersion_FK");
									
									condition = condition + "{'product_FK':'"+pro+"','prodVersion_FK':'"+proVer+"'},";
									
								}
								condition = condition + "{'product_FK':'"+product_FK+"','prodVersion_FK':'"+prodVersion_FK+"'}]";
								
								Ext.fly(this.hidden_MaParam).dom.value = condition;
								
							}
							
							this.hide();
							
							gp_MaParam.load(condition);
							gp_MaReport.load(condition);
						}
					}
				},{
					text: "关闭",
					scope: this,
					handler: function(){
						this.hide();
					}
				}]
			});
		}
	});
	
/****************************************************
 * @class MaProduVerGrid
 * @extends Ext.grid.GridPanel
 ***************************************************/
	MaProduVerGrid = Ext.extend(Ext.grid.GridPanel,{
		condition: null,
		store: null,
		cm: null,
		searchField: null,
		constructor: function(config){
			Ext.apply(this,config);
			
			this.condition = {};
			this.condition.limit = 100;
			
			this.store = new Ext.data.Store({
			    baseParams: this.condition,   
				proxy: new Ext.data.HttpProxy({
					method: 'POST',
					url: '/pdm/report/queryGpParam.htm'
				}),
				reader: new Ext.data.JsonReader({
					totalProperty: 'totalProperty',
					root: 'root',
					id: 'product_FK'
				},[
					{name: 'product_FK'},
					{name: 'prodVersion_FK'}
				])
			});
			
			this.cm = new Ext.grid.ColumnModel([
		        {
		           header: "成品料号",
		           dataIndex: 'product_FK',
		           sortable: true,
		           width: 80
		        },{
		           header: "版本",
		           dataIndex: 'prodVersion_FK',
		           width: 50
		        }]);
		    	        
	        this.searchField = new Ext.ux.form.SearchField({store: this.store, width: 200});
	        
			MaProduVerGrid.superclass.constructor.call(this, {
				enableColumnMove: false,
				enableHdMenu: false,
			    loadMask: {msg: "信息载入中..."},
			    viewConfig: {forceFit: true},
				region: 'center',
				border: false,
				height: 500,
				layout: 'fit',
				split: true,
				stripeRows: true,
				ds: this.store,
				sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
				cm: this.cm,
				trackMouseOver: false,
				tbar: ["成品料号：",this.searchField],
				bbar: new Ext.PagingToolbar({
					pageSize: 100,
					store: this.store,
					displayInfo: true
				})
			});
		},
		load: function(){
			this.store.load();
		}
	});
